﻿#include <iostream>
#include "threadpool.h"

void test1()
{
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    for (int i = 0; i < 5; i++)
    {
        printf("test1 i=%d id=%d\n", i, std::this_thread::get_id());
    }
}

void test2(int a, int b, int c, std::string str)
{
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    int ret = a + b + c;

    for (int i = 0; i < 5; i++)
    {
        printf("%s i=%d %d id=%d\n", str.c_str(), i, ret, std::this_thread::get_id());
    }
}

void test3(int a, const char *buf, std::string &str, bool &stop)
{
    for (int i = 0; i < 5; i++)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
        printf("%s a=%d i=%d %s id=%d\n", buf, a, i, str.c_str(), std::this_thread::get_id());

        if(stop)
            break;
    }
}

int main()
{
    printf("MainThread id=%d\n", std::this_thread::get_id());

    ThreadPool pool;
    pool.initThreadPool(1);

    //添加任务1
    auto testFunc = []() {test1(); };
    pool.commitTask(testFunc);

    //添加任务2
    int a = 10;
    int b = 20;
    int c = 30;
    std::string info = "lambda";
    auto testFunc1 = [=](){test2(a, b, c, info);};
    pool.commitTask(testFunc1);

    //添加任务3
    info = "templete";
    pool.commitTaskEx(test2, a, b, c, info);

    //添加任务4
    char buf[10] = "test3";
    std::string str = "ceshi123";
    pool.commitTaskEx(test3, 4, buf, str, std::ref(pool.m_stop));

    std::this_thread::sleep_for(std::chrono::milliseconds(8000));
    return 0;
}
